package com.flyaudio.xiaopengautopilot.view;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.flyaudio.xiaopengautopilot.R;
import com.flyaudio.xiaopengautopilot.constant.JSONKeyName;
import com.flyaudio.xiaopengautopilot.presenter.IActivityCallback;
import com.flyaudio.xiaopengautopilot.presenter.IVehicleModule;
import com.flyaudio.xiaopengautopilot.util.JSONUtil;
import com.flyaudio.xiaopengautopilot.util.L;
import com.flyaudio.xiaopengautopilot.constant.MsgViewUtil;

public class ConditionDialog extends Dialog  {
    private final String TAG = "ConditionDialog";

    private Context context;
    private int resLayout;

    private TextView vehicle_total_mileage;
    private TextView vehicle_mileage_lastcharge;
    private TextView vehicle_mileage_autorun;

    public ConditionDialog(Context context) {
        super(context);
    }

    public ConditionDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    public ConditionDialog(Context context, int theme, int resLayout) {
        super(context, theme);
        this.context = context;
        this.resLayout = resLayout;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(resLayout);

        Window dialogWindow = getWindow();
        WindowManager manager = ((Activity) context).getWindowManager();
        WindowManager.LayoutParams params = dialogWindow.getAttributes();
        dialogWindow.setGravity(Gravity.BOTTOM);
        initView();
        updateView();
        L.d(TAG,"onCreate");
    }

    private void initView() {
        vehicle_total_mileage = findViewById(R.id.vehicle_total_mileage);
        vehicle_mileage_lastcharge = findViewById(R.id.vehicle_tpms_temp_lf);
        vehicle_mileage_lastcharge = findViewById(R.id.vehicle_mileage_lastcharge);
        vehicle_mileage_autorun = findViewById(R.id.vehicle_mileage_autorun);
    }

    public void updateView() {
        double totalMileage = Double.parseDouble(JSONUtil.getInstance().getMileageJSON(JSONKeyName.TotalMileage,"0").toString())*0.001;
        vehicle_total_mileage.setText(String.format("%.1f", totalMileage));

        setChargingMileageView(totalMileage);
        setAutoRunMileageView(totalMileage);
    }

    private void setChargingMileageView(double totalMileage) {
        double lastChargeMileage = Double.parseDouble(JSONUtil.getInstance().getMileageJSON(JSONKeyName.LastChargeMileage,"0").toString())*0.001;
        double tempVal = totalMileage-lastChargeMileage;
        vehicle_mileage_lastcharge.setText(String.format("%.1f", tempVal));
    }

    private void setAutoRunMileageView(double totalMileage) {
        double autoRunMileage =  Double.parseDouble(JSONUtil.getInstance().getMileageJSON(JSONKeyName.AutoRunMileage,"0").toString())*0.001;
        double tempVal = totalMileage-autoRunMileage;
        vehicle_mileage_autorun.setText(String.format("%.1f",tempVal));
    }


}
